package com.yuanzi.base.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Random;

/**
 * @Description:字符串工具类
 * @author xueyuanyuan
 * @date:2019年4月25日 下午3:54:48
 */
public class StringUtils {

	/**
	 * 判断字符串是否为空字符串
	 * @author xueyuanyuan
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if(str == null || str.trim().equals("") || str.trim().equalsIgnoreCase("null")){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断字符串长度是否合法
	 * @author xueyuanyuan
	 * @param str
	 * @param start 最小长度
	 * @param end 最大长度
	 * @return
	 */
	public static boolean isValidLenght(String str, int start, int end) {
		if(str == null) return false;
		if(str.trim().length() < start || str.trim().length() > end) {
			return false;
		}
		return true;
	}
	
	/**
	 * 生成随机字符串
	 * @author xueyuanyuan
	 * @param length 要生成的字符串的长度
	 * @return
	 */
	public static String getRandomStr(int length){
		if(length <= 0) return "";
		String baseStr = "abcdefghijklmnopqrstuvwxyz0123456789";     
	    Random random = new Random();
	    StringBuffer stringBuffer = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(baseStr.length());     
	        stringBuffer.append(baseStr.charAt(number));     
	    }
	    return stringBuffer.toString();     
	}
	
	/**
	 * 生成随机数字串
	 * @author xueyuanyuan
	 * @param length 要生成的数字的长度
	 * @return
	 */
	public static String getRandomNum(int length){
		if(length <= 0) return "";
		String baseStr = "0123456789";     
	    Random random = new Random();
	    StringBuffer stringBuffer = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(baseStr.length());     
	        stringBuffer.append(baseStr.charAt(number));     
	    }
	    return stringBuffer.toString();     
	}
	
	/**
	 * 截取链接的基础地址
	 * @author xueyuanyuan
	 * @param url 完整的url，如http://localhost:8080?param=value https://tapi.seentao.com/school/ss.get?param.value
	 * @return 基础地址，如http://localhost:8080
	 */
	public static String getBaseUrl(String url) {
		URL objUrl;
		try {
			objUrl = new URL(url);
			return objUrl.getHost();
		} catch (MalformedURLException e) {
			return url;
		}
//		url = url.split("\\?")[0];
//		if(url.endsWith("/")) {
//			url = url.substring(0, url.length() - 1);
//		}
//		return url;
	}
	
	/**
	 * 长整型转字符串
	 * @author xueyuanyuan
	 * @param num
	 * @return
	 */
	public static String long2Str (Long num) {
		String str = "";
		if(num != null) {
			str = String.valueOf(num);
		}
		return str;
	}
	
	/**
	 * 整型转字符串
	 * @author xueyuanyuan
	 * @param num
	 * @return
	 */
	public static String int2Str (Integer num) {
		String str = "";
		if(num != null) {
			str = String.valueOf(num);
		}
		return str;
	}
	
	/**
	 * 初始化字符串，null返回""
	 * @author xueyuanyuan
	 * @param str
	 * @return
	 */
	public static String formatStr(String str) {
		return str == null ? "" : str;
	}
	
	/**
	 * 数值型List以符号分隔返回
	 * @author xueyuanyuan
	 * @param list 数值泛型List
	 * @param separator 分隔符
	 * @return
	 */
	public static String list2Str(List<? extends Number> list, String separator) {
		StringBuilder strBuilder = new StringBuilder();
		if(list != null && list.size() >= 0 && !isEmpty(separator)) {
			list.forEach(item -> {
				strBuilder.append(item);
				strBuilder.append(separator);
			});
		}
		return strBuilder.length() > 0 ? strBuilder.substring(0, strBuilder.length() - 1) : strBuilder.toString();
	}
	
	/**
	 * 数值型List以逗号分隔返回
	 * @author xueyuanyuan
	 * @param list 数值泛型List
	 * @param separator 分隔符
	 * @return
	 */
	public static String list2Str(List<? extends Number> list) {
		return list2Str(list, ",");
	}
	
}
